<template>
  <div class="login_container">
    <div class="title">
      <img class="title_img" src="../assets/login_title.png" />
    </div>
    <div class="login_box">
      <el-form class="form" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="0px">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user"  v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-view" type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item class="login">
          <el-button  @click="submitForm('ruleForm')">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div class="version">哈哈版权所有</div> -->
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      var validatePhone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          password: '123456',
          username: 'user1',
        },
        rules: {
          username: [{
            validator: validatePhone,
            trigger: 'blur'
          }],
          password: [{
            validator: validatePass,
            trigger: 'blur'
          }]
        }
      };
    },

    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async valid => {
          if (valid) {
            const res = await this.$http.post("/api/auth/login",this.ruleForm);
            console.log(res)
            if(res.data.code == 200) {
              this.$message.success("登陆成功");
              // 保存token
              console.log(res.data.data.token)

              window.sessionStorage.setItem("token",res.data.data.token);
              this.$router.push('/home');
            }else {
              this.$message.error(res.data.msg);
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
  .login_container {
    background: #2b4b6b;
    background-image: url(../assets/login_bg.png);
    position: fixed;
    width: 100%;
    height: 100%;
    background-size: 100% 100%;
  }

  .title {
    margin-top: 120px;
    text-align: right;
    margin-right: 200px;
  }

  .title_img {
    width: 400px;
    height: 70px;
  }

  .login_box {
    width: 320px;
    /* height: 180px; */
    /* background-image: url(../assets/login_box.png); */
    background-color: rgba(255.0, 255.0, 255.0, .3);
    border-radius: 12px;
    position: absolute;
    top: 250px;
    right: 220px;
    padding: 30px;
  }
  .form {
    margin-top: 30px;
  }
  .login {
    text-align: center;
  }
  .version {
    position: absolute;
    color: #FFFFFF;
    font-size: 24px;
    top: 540px;
    right: 450px;
  }

</style>
